<template>
  <!-- 评论的每一个组件 -->
  <van-cell class="comment-item">
    <van-image
      slot="icon"
      class="avatar"
      round
      fit="cover"
      :src="coment.aut_photo"
    />
    <div slot="title" class="title-wrap">
      <div class="user-name">{{ coment.aut_name }}</div>
      <van-button
        class="like-btn"
        :icon="coment.is_liking ? 'good-job' : 'good-job-o'"
        :class="{ liked: coment.is_liking }"
        @click="addClick"
        >{{ coment.reply_count || "赞" }}</van-button
      >
    </div>

    <div slot="label">
      <p class="comment-content">{{ coment.content }}</p>
      <div class="bottom-info">
        <span class="comment-pubdate">{{ coment.pubdate | dataXiangdui }}</span>
        <van-button class="reply-btn" round @click="isPopupShow = true"
          >回复 {{ coment.reply_count }}</van-button
        >
        <!-- 写评论弹出层 -->
        <van-popup v-model="isPopupShow" position="bottom"
          ><ComentPost @isPingShow="isPingShow" :artId="coment.com_id"
        /></van-popup>
        <!-- /写评论弹出层 -->
      </div>
    </div>
  </van-cell>
</template>

<script>
import { addZan, delZan } from "@/api/coment.js";
import ComentPost from "./coment-post.vue";

export default {
  name: "CommentItem",
  components: {
    ComentPost,
  },
  props: {
    coment: {
      type: [Object],
      required: true,
    },
  },
  data() {
    return {
      isPopupShow: false,
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 点赞 评论
    async addClick() {
      console.log(this.coment);

      try {
        if (this.coment.is_liking) {
          const { data: res } = await delZan(this.coment.com_id);
          this.coment.is_liking = false;
          this.comments.reply_count--;

          console.log(res);
        } else {
          const { data: res } = await addZan(this.coment.com_id);
          this.coment.is_liking = true;
          this.comments.reply_count++;
          console.log(res);
        }
      } catch (err) {
        console.log(err);
        if (err.request && err.request.status === 500) {
          this.$toast("服务器错误");
        }
      }
    },
    // 关闭评论弹出层
    isPingShow() {
      this.isPopupShow = false;
    },
  },
};
</script>

<style scoped lang="less">
.comment-item {
  .avatar {
    width: 72px;
    height: 72px;
    margin-right: 25px;
  }
  .title-wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .user-name {
      color: #406599;
      font-size: 26px;
    }
  }
  .comment-content {
    font-size: 32px;
    color: #222222;
    word-break: break-all;
    text-align: justify;
  }
  .comment-pubdate {
    font-size: 19px;
    color: #222;
    margin-right: 25px;
  }
  .bottom-info {
    display: flex;
    align-items: center;
  }
  .reply-btn {
    width: 135px;
    height: 48px;
    line-height: 48px;
    font-size: 21px;
    color: #222;
  }
  .like-btn {
    height: 30px;
    padding: 0;
    border: none;
    font-size: 19px;
    line-height: 30px;
    margin-right: 7px;
    .van-icon {
      font-size: 30px;
    }
    &.liked {
      color: #e5645f;
    }
  }
}
</style>